package beanLifeCycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {
	
	
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if("car".equals(beanName)) {
			Car car = (Car)bean;
			if(null == car.getColor()) {
				System.out.println("调用 BeanPostProcessor.postProcessBeforeInitialization(), color 为空,设置默认黑色 ");
				car.setColor("黑色");
			}
		}
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if("car".equals(beanName)) {
			Car car = (Car)bean;
			if(car.getMaxSpeed()>=200) {
				System.out.println("调用 BeanPostProcessor.postProcessAfterInitialization(), 将maxSpeed调整为200 ");
				car.setMaxSpeed(200);
			}
		}
		return bean;
	}
}
